﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Kimd.Motion
{
    public class PositionLimit
    {
        public static Dictionary<string, Axis> MachineAxis;
        public static Dictionary<string, Sensor> MachineSensor;
        private Axis _thisAxis;
        public double SectionStartPosition { get; set; }
        public double SectionEndPosition { get; set; }
        public string LimitAxisName { get; set; }
        public double LimitStartPosition { get; set; }
        public double LimitEndPosition { get; set; }
        public string RelatedAxisName { get; set; }
        public double RelatedStartPosition { get; set; }
        public double RelatedEndPosition { get; set; }
        [XmlIgnore]
        public bool IsEnable { get; set; } = true;

        public PositionLimit()
        {

        }
        public void SetAxis(Axis axis)
        {
            _thisAxis = axis;
        }
        public static void Init(Dictionary<string, Axis> machineAxis, Dictionary<string, Sensor> machineSensor)
        {
            MachineAxis = machineAxis;
            MachineSensor = machineSensor;
        }
        public bool IsBreakRule(bool moveDirection, out string errInfo)
        {
            errInfo = "";
            if (!IsEnable)
                return false;
            if(!string.IsNullOrEmpty(RelatedAxisName))
            {
                if (!MachineAxis.ContainsKey(RelatedAxisName))
                {
                    errInfo = "轴限制条件设置错误!相关轴不存在";
                    return true;
                }
                if (string.IsNullOrEmpty(LimitAxisName))
                {
                    errInfo = "轴限制条件设置错误!限制轴未设置";
                    return true;
                }
                if (!MachineAxis.ContainsKey(LimitAxisName))
                {
                    errInfo = "轴限制条件设置错误!限制轴不存在";
                    return true;
                }
                //限位轴在不安全区间时
                if (MachineAxis[LimitAxisName].Position >= LimitStartPosition && MachineAxis[LimitAxisName].Position <= LimitEndPosition)
                {
                    //当前轴在位置下限附近，可以往负向移动
                    if (Math.Abs(_thisAxis.Position - SectionStartPosition) < 5 && !moveDirection)
                    {
                        return false;
                    }
                    //当前轴在位置上限附近，可以往正向移动
                    if (Math.Abs(_thisAxis.Position - SectionEndPosition) < 5 && moveDirection)
                    {
                        return false;
                    }
                    //相关轴在关联范围内时，限制起作用
                    if ((_thisAxis.Position >= SectionStartPosition && _thisAxis.Position <= SectionEndPosition) && (MachineAxis[RelatedAxisName].Position >= RelatedStartPosition && MachineAxis[RelatedAxisName].Position <= RelatedEndPosition))
                    {
                        errInfo = $"限制轴:{LimitAxisName}在[{LimitStartPosition}]-[{LimitEndPosition}]范围内时,轴:{_thisAxis.Description}试图进入[{SectionStartPosition}]-[{SectionEndPosition}]范围";
                        return true;
                    }
                }
                else
                    return false;
            }
            else
            {
                if (string.IsNullOrEmpty(LimitAxisName))
                {
                    errInfo = "轴限制条件设置错误!限制轴未设置";
                    return true;
                }
                if (!MachineAxis.ContainsKey(LimitAxisName))
                {
                    errInfo = "轴限制条件设置错误!限制轴不存在";
                    return true;
                }
                //限位轴在不安全区间时
                if (MachineAxis[LimitAxisName].Position >= LimitStartPosition && MachineAxis[LimitAxisName].Position <= LimitEndPosition)
                {
                    //当前轴在位置下限附近，可以往负向移动
                    if(Math.Abs(_thisAxis.Position - SectionStartPosition) < 5 && !moveDirection)
                    {
                        return false;
                    }
                    //当前轴在位置上限附近，可以往正向移动
                    if (Math.Abs(_thisAxis.Position - SectionEndPosition) < 5 && moveDirection)
                    {
                        return false;
                    }
                    if (_thisAxis.Position >= SectionStartPosition && _thisAxis.Position <= SectionEndPosition)
                    {
                        errInfo = $"限制轴:{LimitAxisName}在[{LimitStartPosition}]-[{LimitEndPosition}]范围内时,轴:{_thisAxis.Description}试图进入[{SectionStartPosition}]-[{SectionEndPosition}]范围";
                        return true;
                    }
                }
                else
                    return false;
            }
            return false;
        }
    }
}
